The instance of `obj` at number n, counting from 0, or nothing if n is below 0 or there are not that many. If `obj` is a parent, its children count as `obj`.
instance_nth(obj, n) -> ref<object>| Name | Type |
|---|---|
obj | object |
n | int |
ref<object>
Gives you one of the live instances of an object, picked by a number, or nothing if there is no instance with that number.
The first one is number 0, the same way the first place in an array, a list or a string is 0. So instance_nth(obj_enemy, 0) is the first enemy and instance_nth(obj_enemy, 2) is the third. With three enemies alive, the numbers that work are 0, 1 and 2 - that is, 0 up to instance_count(obj) - 1. A number below 0, or one that is too big, gives nothing. It never crashes, so you can ask for number 5 without checking how many there are first, as long as you check the answer.
If obj is a parent, its children count as obj too. With obj_enemy as the parent of obj_slime and obj_bat, the numbers run through the plain enemies first, then the slimes, then the bats, and instance_count(obj_enemy) is how many numbers there are in all. The reference you get back is typed as obj_enemy, so you can use the variables and events obj_enemy has through it.
This is the thing to know before you use it. Number 0 is not "the oldest instance". The game keeps each object's instances in a row of fixed places, and a new instance takes the first empty place. When an instance is destroyed its place becomes empty, and the next one you create moves into that gap instead of going on the end.
So with three enemies alive, if the first one is destroyed and a new one is created once that frame has ended, the new one is number 0, not number 3.
Only two things are promised:
instance_count(obj) - 1 reach every live instance exactly once.Do not rely on anything more than that, such as "number 1 is the second one I made". And do not remember a number to find an instance again later. Keep the reference that instance_nth gave you instead. A reference also tells you when its instance has gone, which a number cannot.
An instance destroyed earlier in the same frame still has a number until the end of the frame, the same way instance_count still counts it.
Each call counts along the object's instances to get to number n (to the last of 32 instances is about 2,500 cycles, under 1% of a frame). Asking for every number in a loop therefore does that counting again for each one. To do something to every instance of an object, use with (obj_enemy) { ... } instead. It visits each one exactly once. instance_nth is for the times you want one particular number, for example the first three instances, or one picked at random with instance_nth(obj, roll_int(0, instance_count(obj) - 1)) (roll_int).
for (var i = 0; i < 3; i += 1) {
var e = instance_nth(obj_doc_enemy, i);
if (e != nothing) {
e.hp += 5;
}
}Heals the first three enemies by 5 health each. If fewer than three are alive, instance_nth gives nothing for the missing ones and the if skips them.
Numbers run from 0 to instance_count(obj) - 1, so `instance_nth(obj, 0)` is the first one, the same counting an array or a string index uses. Anything outside that - below 0, or past the last one - gives `nothing`, never a crash. THE ORDER IS NOT CREATION ORDER. The numbers follow the slots the object’s instances sit in, and a new instance takes the LOWEST free slot - so once any instance has been destroyed, the next one created fills that gap instead of joining the end. Only two things are promised: while nothing is created or destroyed, the same n gives the same instance, and 0 up to instance_count(obj) - 1 reaches every live instance exactly once. Never rely on "number 2 is the third one I made", and never keep a number to find an instance again later - keep the reference itself, which also tells you when the instance has gone. For a parent, the parent’s own instances come first and then each child’s, and instance_count(obj) counts them all, so the range is the same. An instance destroyed earlier in this same frame still counts, exactly as it does for instance_count. Each call counts along the pool to reach n, so visiting every instance with a loop of instance_nth calls does that walk once per call; `with (obj) { ... }` visits each one exactly once and is the better tool for that. Counting along to the last of 32 instances costs about 2,500 cycles, under 1 percent of a frame.